package com.bigdata.demo.web.controller;

import com.bigdata.demo.base.ApiDataTableResponse;
import com.bigdata.demo.base.ApiResponse;
import com.bigdata.demo.service.ServiceMultiResult;
import com.bigdata.demo.service.ServiceResult;
import com.bigdata.demo.service.letter.ISearchDocService;
import com.bigdata.demo.service.search.ISearchService;
import com.bigdata.demo.web.dto.CRDTO;
import com.bigdata.demo.web.dto.ECMLetterDTO;
import com.bigdata.demo.web.form.CRReportSearch;
import com.bigdata.demo.web.form.ECMLetterSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class ECMDocController {

    @Autowired
    private ISearchDocService  serviceDocService;

    /**
     * 自动补全接口
     */
    @GetMapping("search/letter/autocomplete")
    @ResponseBody
    public ApiResponse autocomplete(@RequestParam(value = "prefix") String prefix) {

        if (prefix.isEmpty()) {
            return ApiResponse.ofStatus(ApiResponse.Status.BAD_REQUEST);
        }
        ServiceResult<List<String>> result = this.serviceDocService.suggest(prefix);
        return ApiResponse.ofSuccess(result.getResult());
    }
    @PostMapping("search/letter")
    @ResponseBody
    public ApiDataTableResponse crsForAPI(@ModelAttribute ECMLetterSearch searchBody) {
        ServiceMultiResult<ECMLetterDTO> result = serviceDocService.query(searchBody);

        ApiDataTableResponse response = new ApiDataTableResponse(ApiResponse.Status.SUCCESS);
        response.setData(result.getResult());
        response.setRecordsFiltered(result.getTotal());
        response.setRecordsTotal(result.getTotal());

        return response;
    }


}
